﻿// ////////////////////////////////////////////////////////////////////////////
// 
// SWE Case Tool
// 
// Solution:	CaseTool
// Project:		3. MethodPercentage
// Filename:	FrmPercentageMethod.cs
// 
// File Created:	17:15 30.05.2013 
// Last Changed:	18:11 05.06.2013
// 
// ////////////////////////////////////////////////////////////////////////////

using System;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;

namespace SWE.CaseTool.Methods
{
	/// <summary>
	/// Method GUI for the Percentage Method. Offers input for the project phases weights and for a
	/// reference project's phases wheights (optional).
	/// </summary>
	[MethodGUI(typeof (PercentageMethod))]
	public partial class FrmPercentageMethod : Form, IMethodGUI
	{
		/// <summary>
		/// The _percentage method behind this gui.
		/// </summary>
		private PercentageMethod _percentage;

		/// <summary>
		/// Initializes a new instance of the <see cref="FrmPercentageMethod" /> class.
		/// </summary>
		public FrmPercentageMethod()
		{
			InitializeComponent();

			// read the enum values per reflection for the phase selection combobox.
			cbPhases.Items.AddRange(Enum.GetValues(typeof (PercentagePhase)).Cast<object>().ToArray());
		}

		/// <summary>
		/// Sets the method which should be displayed in the gui. The method factory creates or loads a method
		/// and hands it, while creating the window, over to the window using this method.
		/// </summary>
		/// <param name="method">The method.</param>
		/// <exception cref="System.ArgumentException">
		/// Corrupt data. Unable to load the given percentage method
		/// data.
		/// </exception>
		public void SetMethod(IMethod method)
		{
			_percentage = method as PercentageMethod;
			if (_percentage == null)
			{
				throw new ArgumentException("Corrupt data. Unable to load the given percentage method data.");
			}

			numCon.Value = (decimal) _percentage.PartConcept;
			numCode.Value = (decimal) _percentage.PartCoding;
			numTest.Value = (decimal) _percentage.PartTesting;
			numDef.Value = (decimal) _percentage.PartDefinition;
			numRefConcept.Value = (decimal) _percentage.PartRefConcept;
			numRefCode.Value = (decimal) _percentage.PartRefCoding;
			numRefTest.Value = (decimal) _percentage.PartRefTesting;
			numRefDef.Value = (decimal) _percentage.PartRefDefinition;

			numDurRefPhase.Value = (decimal) _percentage.DurationReferencePhase;
			cbPhases.SelectedIndex = (int) _percentage.Phase;

			cbRefProject.Checked = _percentage.UseReferenceProject;
			CbRefProjectCheckedChanged(this, EventArgs.Empty);
		}

		/// <summary>
		/// Returns the mehtods which is displayed in this gui.
		/// </summary>
		/// <returns>
		/// the method.
		/// </returns>
		public IMethod GetMethod() { return _percentage; }

		/// <summary>
		/// Handles the CheckedChanged event of the cbRefProject control. Enables or disables the controls for
		/// the reference project and recalculates, because the results may have changed.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">
		/// The <see cref="EventArgs" /> instance containing the event data.
		/// </param>
		private void CbRefProjectCheckedChanged(object sender, EventArgs e)
		{
			_percentage.UseReferenceProject = cbRefProject.Checked;
			numRefCode.Enabled = cbRefProject.Checked;
			numRefConcept.Enabled = cbRefProject.Checked;
			numRefDef.Enabled = cbRefProject.Checked;
			numRefTest.Enabled = cbRefProject.Checked;
			label3.Visible = cbRefProject.Checked;
			Calculate();
		}

		/// <summary>
		/// Recalculates the percentage method based on the actual data.
		/// </summary>
		private void Calculate()
		{
			try
			{
				tbResult.Text = string.Format("{0} Tage", _percentage.Calculate().ToString(CultureInfo.InvariantCulture));
				lblRes.Text = string.Format("Prozentsatzmethode berechnet: {0}", tbResult.Text);
			}
			catch (Exception ex)
			{
				lblRes.Text = string.Format("ERROR: {0}", ex.Message.ToUpper());
			}
		}

		/// <summary>
		/// Handles the ValueChanged event of the numDurRefPhase control. DataBinding: Takes the new value and
		/// writes it back to the data field in the percentage method. Recalculates the results, after changing
		/// the data.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">
		/// The <see cref="EventArgs" /> instance containing the event data.
		/// </param>
		private void NumDurRefPhaseValueChanged(object sender, EventArgs e)
		{
			_percentage.DurationReferencePhase = (double) numDurRefPhase.Value;
			Calculate();
		}

		/// <summary>
		/// Handles the ValueChanged event of the numDef control. DataBinding: Takes the new value and
		/// writes it back to the data field in the percentage method. Recalculates the results, after changing
		/// the data.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">
		/// The <see cref="EventArgs" /> instance containing the event data.
		/// </param>
		private void NumDefValueChanged(object sender, EventArgs e)
		{
			_percentage.PartDefinition = (double) numDef.Value;
			Calculate();
		}

		/// <summary>
		/// Handles the ValueChanged event of the numCon control. DataBinding: Takes the new value and
		/// writes it back to the data field in the percentage method. Recalculates the results, after changing
		/// the data.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">
		/// The <see cref="EventArgs" /> instance containing the event data.
		/// </param>
		private void NumConValueChanged(object sender, EventArgs e)
		{
			_percentage.PartConcept = (double) numCon.Value;
			Calculate();
		}

		/// <summary>
		/// Handles the ValueChanged event of the numCode control. DataBinding: Takes the new value and
		/// writes it back to the data field in the percentage method. Recalculates the results, after changing
		/// the data.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">
		/// The <see cref="EventArgs" /> instance containing the event data.
		/// </param>
		private void NumCodeValueChanged(object sender, EventArgs e)
		{
			_percentage.PartCoding = (double) numCode.Value;
			Calculate();
		}

		/// <summary>
		/// Handles the ValueChanged event of the numTest control. DataBinding: Takes the new value and
		/// writes it back to the data field in the percentage method. Recalculates the results, after changing
		/// the data.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">
		/// The <see cref="EventArgs" /> instance containing the event data.
		/// </param>
		private void NumTestValueChanged(object sender, EventArgs e)
		{
			_percentage.PartTesting = (double) numTest.Value;
			Calculate();
		}

		/// <summary>
		/// Handles the ValueChanged event of the numRefDef control. DataBinding: Takes the new value and
		/// writes it back to the data field in the percentage method. Recalculates the results, after changing
		/// the data.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">
		/// The <see cref="EventArgs" /> instance containing the event data.
		/// </param>
		private void NumRefDefValueChanged(object sender, EventArgs e)
		{
			_percentage.PartRefDefinition = (double) numRefDef.Value;
			Calculate();
		}

		/// <summary>
		/// Handles the ValueChanged event of the numRefConcept control. DataBinding: Takes the new value and
		/// writes it back to the data field in the percentage method. Recalculates the results, after changing
		/// the data.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">
		/// The <see cref="EventArgs" /> instance containing the event data.
		/// </param>
		private void NumRefConceptValueChanged(object sender, EventArgs e)
		{
			_percentage.PartRefConcept = (double) numRefConcept.Value;
			Calculate();
		}

		/// <summary>
		/// Handles the ValueChanged event of the numRefCode control. DataBinding: Takes the new value and
		/// writes it back to the data field in the percentage method. Recalculates the results, after changing
		/// the data.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">
		/// The <see cref="EventArgs" /> instance containing the event data.
		/// </param>
		private void NumRefCodeValueChanged(object sender, EventArgs e)
		{
			_percentage.PartRefCoding = (double) numRefCode.Value;
			Calculate();
		}

		/// <summary>
		/// Handles the ValueChanged event of the numRefTest control. DataBinding: Takes the new value and
		/// writes it back to the data field in the percentage method. Recalculates the results, after changing
		/// the data.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">
		/// The <see cref="EventArgs" /> instance containing the event data.
		/// </param>
		private void NumRefTestValueChanged(object sender, EventArgs e)
		{
			_percentage.PartRefTesting = (double) numRefTest.Value;
			Calculate();
		}

		/// <summary>
		/// Handles the SelectedIndexChanged event of the cbPhases control. DataBinding: Takes the new value and
		/// writes it back to the data field in the percentage method. Recalculates the results, after changing
		/// the data.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">
		/// The <see cref="EventArgs" /> instance containing the event data.
		/// </param>
		private void CbPhasesSelectedIndexChanged(object sender, EventArgs e)
		{
			_percentage.Phase = (PercentagePhase) cbPhases.SelectedItem;
			Calculate();
		}

		/// <summary>
		/// Handles the FormClosed event of the FrmPercentageMethod control. Disposes the percentage method
		/// while closing. While disposing the percentage method will save itselfs back in the project global
		/// dictionary.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">
		/// The <see cref="FormClosedEventArgs" /> instance containing the event data.
		/// </param>
		private void FrmPercentageMethod_FormClosed(object sender, FormClosedEventArgs e) { _percentage.Dispose(); }
	}
}